import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt, pyqtSignal
from .slave_trait_cell import SlaveTraitCell
from ..data.trait import Trait

class SlaveTraitWidget(QWidget):
	mouse_in = pyqtSignal(bool)
	mouse_out = pyqtSignal(bool)

	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)
#
#		tweak these to control placement of the cells#
#		these can be murder to get right, so be warned
#		41/3 seem to work at the moment
#
		self.row_height = 41
		self.top_margin = 3
#
#		frame for the table - should hold 10 cells
#
		path = "resources/slave_market_trait_frame.png"
		bg = QtGui.QLabel(self)
		bg.setPixmap(QtGui.QPixmap(path))
		bg.setGeometry(0, 0, 238, 450)

		self.cells = []
		self.girl = None
		self.current = 0
#
#		OK. Now want 10 permanent table cells 
#
		for i in range(10):
			cell = SlaveTraitCell(self)
			cell.move(21,20 + 41 * i)
#
#			mouseover signals
#			should look at doing this with one
#			signal sending true/false
#
			cell.mouse_in.connect(self.cell_enter)
			cell.mouse_out.connect(self.cell_exit)
#
#			add the cell to a list
#
			self.cells.append(cell)
#
#		cell 10 gets some special consideratoin. 
#		it's the "more" button for one thing
#
		self.cells[9].clicked.connect(self.more)
#
#		more mode is true if we have more than 10
#		traits to display. In that event, cell10
#		becomes a more button and we need to count
#		pages
#
		self.more_mode = False


#
#	this gets called when we mouse out
#	of a cell. Sets the trait description 

#
	def cell_exit(self):
		self.current_desc = ""
		self.mouse_out.emit(True)

#
#	when the mouse rolls in, we set the descrition
#	so the monitor can display it
#
	def cell_enter(self, index):
#
#		find which cell 
#
		cell = self.sender()
		text = cell.text()
#
#		If the cell displays "~~~ More ~~~"
#		then it's not a trait. So clear
#		the description 
#
		if text == "~~~ More ~~~":
			text = ""
#
#		and if text is "" then don't send an event
#		we#ve nothing to say, so let's not say it
#
		if text == "":
			return
#
#		format the description
#
		self.current_desc  = "{}\n{}".format(
			cell.text(), cell.desc
		)
		self.mouse_in.emit(True)
		

#
#	populate the trait cells given a girl object
#
	def set_traits(self, girl):
#
#		reset the page and "more mode" flag
#
		self.page = 0
		self.more_mode = False
#
#		store the girl reference
#
		self.girl = girl
#
#		shorthand refs for cell 10 and the trait list
#
		cell10 = self.cells[9]
		l = girl.traits
#
#		loop over a counted lust of traits
#
		for i, t in enumerate(l):
#
#			but only the first nine
#
			if i >= 9:
				break
			cell = self.cells[i]
#
#			set the cell#s name and description
#			from the trait
#
			cell.setText(t.name)
			cell.desc = t.desc
#
#		if we have slots remaining, they may need
#		to be cleared
#
		if len(l) <= 9:
			for i in range(len(l), 10):
				self.cells[i].setText("")
				self.cells[i].desc = None
			return
#
#		if the list is exactly 10 long
#		we use cell 10 as a disply element
#		just like the others
#
		if len(l) == 10:
			s = "{}".format( l[9] )
			self.cells[9].setText(s)
			self.cells[9].desc = t.desc
			return
#
#		otherwise we write "more" into the cell
#		and activate "more mode"
#
		cell10.setText("~~~ More ~~~")
		cell10.desc = None
		self.more_mode = True

#
#	callback functon from when cell 10 gets clicked
#
	def more(self):
#
#		if more mode is not set this does nothing
#		should probably disconnect and reconnect
#		the signal. This works for now.
#
		if not self.more_mode:
			return
#
#		OK, the user wants the next page
#		of traits. Increment the page count
#		("page" meaning a bundle or nine or less
#		trait names in this case)
#
		self.page += 1
#
#		scrub all the old text 
#
		for i in range(0, 9):
			self.cells[i].setText("")
#
#		shorthand for the trait list and
#		the length of that list
#
		traits = self.girl.traits
		tlen = len(traits)
#
#		work out the index to start from
#		if it's greater than the length of the list
#		go back to the start
#
		low = self.page * 9
		if low >= tlen:
			self.page = 0
			low = 0
#
#		now write names and descriptions
#
		for i in range(9):
			idx = low + i
			if idx >= tlen:
				break
			self.cells[i].setText(traits[idx].name)
			self.cells[i].desc = traits[idx].desc


#
# this is a mock girl for testing purposes
# all she has is a list of traits, poor dear
#
class TestGirl(object):
	def __init__(self, traits, *args, **kw):
		object.__init__(self)
		self.traits = traits

if __name__ == "__main__":

	index = 0
	widget = None
	girls = []
#
#	we need a callback for the next button
#	this test is as complicated as some screens...
#
	def next(thing):
		global index
		global girls
		global widget
		index += 1
		if index >= len(girls):
			index = 0
		widget.set_traits(girls[index])
#
#	we need an app
#
	app = QtGui.QApplication(sys.argv)
#
#	make sure gabrielle is in the font db
#
	font_db = QFontDatabase()
	font_db.addApplicationFont("resources/Gabrielle.ttf")
#
#	set background so we can see how the widget looks
#	against wood
#
	path = "resources/wood.jpg"
	top = QtGui.QLabel()
	top.setPixmap(QtGui.QPixmap(path))
	top.setGeometry(0, 0, 1024, 768)
#
#	set up the widget
#
	widget = SlaveTraitWidget(top)
	widget.setGeometry(20, 20, 238, 450)
#
#	let's have some test girls
#
	girls = [
		TestGirl([
			Trait(name = "Busty"),
			Trait(name = "Bouncy"),
			Trait(name = "Bonny"),
			Trait(name = "Bonckable"),
		]),
		TestGirl([
			Trait(name = "Affable"),
			Trait(name = "Bright"),
			Trait(name = "Capable"),
			Trait(name = "Desirable"),
			Trait(name = "Easy"),
			Trait(name = "Fuckable"),
			Trait(name = "Georgeous"),
			Trait(name = "Horny"),
			Trait(name = "Intelligent"),
			Trait(name = "Jolly"),
			Trait(name = "Kind"),
			Trait(name = "Lewd"),
			Trait(name = "Melons"),
			Trait(name = "Nymphomaniac"),
			Trait(name = "Orgasmic"),
			Trait(name = "Placid"),
			Trait(name = "Quiet"),
			Trait(name = "Risque"),
			Trait(name = "Submissive"),
			Trait(name = "Timid"),
			Trait(name = "Unassuming"),
			Trait(name = "Vivacious"),
			Trait(name = "Well Fit"),
			Trait(name = "X-Rated"),
			Trait(name = "Young Looking"),
			Trait(name = "ZHot"),
		]),
		TestGirl([
			Trait(name = "Robot"),
			Trait(name = "Slow Learner"),
			Trait(name = "Tireless"),
		]),
		TestGirl([
			Trait(name = "Edge Case 9"),
			Trait(name = "2"),
			Trait(name = "3"),
			Trait(name = "4"),
			Trait(name = "5"),
			Trait(name = "6"),
			Trait(name = "7"),
			Trait(name = "8"),
			Trait(name = "9"),
		]),
		TestGirl([
			Trait(name = "Edge Case 10"),
			Trait(name = "2"),
			Trait(name = "3"),
			Trait(name = "4"),
			Trait(name = "5"),
			Trait(name = "6"),
			Trait(name = "7"),
			Trait(name = "8"),
			Trait(name = "9"),
			Trait(name = "10"),
		]),
		TestGirl([
			Trait(name = "Edge Case 11"),
			Trait(name = " 2 / 11"),
			Trait(name = " 3 / 11"),
			Trait(name = " 4 / 11"),
			Trait(name = " 5 / 11"),
			Trait(name = " 6 / 11"),
			Trait(name = " 7 / 11"),
			Trait(name = " 8 / 11"),
			Trait(name = " 9 / 11"),
			Trait(name = "10 / 11"),
			Trait(name = "11 / 11"),
		]),
	]
#
#	display the first of 'em
#
	widget.set_traits(girls[index])
#
#	button to show the next girl
#
	button = SlaveTraitCell(top)
	button .setGeometry(40, 500, 200, 42)
	button.setText("Next")
	button.clicked.connect(next)
#
#	and set it all going
#
	top.show()
	sys.exit(app.exec_())

